package com.woniu.cms.goods.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.cms.goods.core.entity.GoodsDictionary;
import com.woniu.cms.goods.core.entity.GoodsType;
import com.woniu.cms.goods.core.mapper.GoodsDictionaryMapper;
import com.woniu.cms.goods.core.service.IGoodsDictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cms.goods.core.service.IGoodsTypeService;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.Impl.BusinessCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 材料字典表，内涵所有材料，所有组织机构共享 服务实现类
 * </p>
 *
 * @author romantic sheep
 * @since 2022-09-17
 */
@Service
public class GoodsDictionaryServiceImpl extends ServiceImpl<GoodsDictionaryMapper, GoodsDictionary> implements IGoodsDictionaryService {
    @Autowired
    private IGoodsTypeService typeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /*
     * 材料字典分类查询方法，前端根据材料类别分别展示材料详情
     * @currentPage 当前页码
     * @pageSize 页面大小
     * @dicId 材料类别id
     * */
    @Override
    public Result getlist(Integer currentPage, Integer pageSize, Integer typeId,Integer status) {
        IPage iPage=new Page(currentPage,pageSize);
        LambdaQueryWrapper<GoodsDictionary> qw = Wrappers.lambdaQuery();
        if (typeId!=null){
            qw.eq(GoodsDictionary::getTypeId,typeId);
        }
        if (status!=null){
            qw.eq(GoodsDictionary::getStatus,status);
        }
        IPage page = this.page(iPage, qw);
        List records = page.getRecords();
        long total = page.getTotal();
        Map map=new HashMap();
        map.put("data",records);
        map.put("total",total);
        Result<Map> common = Result.common(map);
        return common;
    }
    /*
     * 添加材料字典
     * */
    @Override
    public Result addDictionary(GoodsDictionary goodsDictionary) throws JsonProcessingException {
        if (ObjectUtils.isEmpty(goodsDictionary)){
            return Result.error();
        }
        LambdaQueryWrapper<GoodsDictionary> qw = Wrappers.lambdaQuery();
        qw.eq(GoodsDictionary::getGoodsCode,goodsDictionary.getGoodsCode());
        GoodsDictionary one = this.getOne(qw);
        if (!ObjectUtils.isEmpty(one)){
            return Result.error("材料已存在");
        }
        boolean save = this.save(goodsDictionary);
        if (!save){
            return Result.error(BusinessCode.ADD_FAIL);
        }
        return Result.ok(BusinessCode.ADD_SUCCESS);
    }
    /*
     * 修改字典
     * */
    @Override
    public Result updateDictionary(GoodsDictionary goodsDictionary) {
        if (ObjectUtils.isEmpty(goodsDictionary)){
            return Result.error(BusinessCode.MODIFY_FAIL);
        }
        if (goodsDictionary.getStatus()!=0&&goodsDictionary.getStatus()!=3){
            return Result.error("材料已提交无法删除");
        }
        LambdaUpdateWrapper<GoodsDictionary> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(GoodsDictionary::getId,goodsDictionary.getId());
        boolean update = this.update(goodsDictionary, updateWrapper);
        if (!update){
            return Result.error(BusinessCode.MODIFY_FAIL);
        }
        return Result.ok(BusinessCode.MODIFY_SUCCESS);
    }


}
